package com.wkbb.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeUtil {

    private static final Logger logger = LoggerFactory.getLogger(TimeUtil.class);
	/**
	 * 年月日
	 */
	public static final String YYYY_MM_DD = "yyyy-MM-dd";

	/**
	 * 时分秒
	 */
	public static final String HH_MM_SS = "HH:mm:ss";

	/**
	 * 时分
	 */
	public static final String HH_MM = "HH:mm";

	/**
	 * 日
	 */
	public static final String DD = "dd";

	/**
	 * 年月日时分秒
	 */
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 年月日时分
	 */
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

	/** 年-月-日 时:分:秒.毫秒 */
	public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";

	/** 年月日时分秒毫秒 */
	public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

	/** 年月日时分秒 */
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	/** 年月日 */
	public static final String YYYYMMDD = "yyyyMMdd";

	/** 年月日 */
	public static final String YYMMDD = "yyMMdd";
	
	/** 月日 */
	public static final String MM_DD = "MM-dd";

	public static final String CH_YYYY_MM_DD_HH_MM_SS = "yyyy年MM月dd日 HH时mm分ss秒";

	public static Calendar toCalendar(String dateStr) {
		return toCalendar(dateStr, YYYY_MM_DD);
	}

	public static Calendar toCalendar(Timestamp timestamp) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timestamp.getTime());
		return c;
	}

	/**
	 * 将dateStr转化成时间，并格式化成pattern格式
	 * @param dateStr
	 * @param formatStr
	 * @return Calendar
	 */
	public static Calendar toCalendar(String dateStr, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Calendar cal = Calendar.getInstance();
		cal.clear();
		try {
			cal.setTime(format.parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal;
	}


	/**
	 * 按照pattern格式化字符串
	 * @param date
	 * @param formatStr
	 * @return  string
	 */
	public static String toString(Date date, String formatStr) {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.setTime(date);
		return toString(cal, formatStr);
	}

	public static String toString(Timestamp stamp, String formatStr) {
		return toString((Date) stamp, formatStr);
	}


	public static String toString(Calendar cal, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(cal.getTime());
	}

	public static String toString(long ms, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(ms);
	}

	public static String toString(long ms, String formatStr, int num) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(ms + num * 24 * 60 * 60 * 1000);
	}
	
	/**
	 * 返回 YYYY_MM_DD_HH_MM_SS 格式字符串
	 * @param date
	 * @return YYYY_MM_DD_HH_MM_SS 格式字符串
	 */
	public static String toString(Date date) {
		return toStringYYYY_MM_DD_HH_MM_SS(date);
	}
	
	/**
	 * 将Timestamp 格式化成YYYY_MM_DD格式的字符串
	 * @param stamp
	 * @return YYYY_MM_DD格式的字符串
	 */
	public static String toStringYYYY_MM_DD(Timestamp stamp) {
		return toString(stamp, YYYY_MM_DD);
	}

	/**
	 * 将Timestamp 格式化成YYYY_MM_DD_HH_MM_SS格式的字符串
	 * @param stamp
	 * @return YYYY_MM_DD_HH_MM_SS格式的字符串
	 */
	public static String toStringYYYY_MM_DD_HH_MM_SS(Timestamp stamp) {
		return toString(stamp, YYYY_MM_DD_HH_MM_SS);
	}

	public static String toStringYYYY_MM_DD(Date date) {
		return toString(date, YYYY_MM_DD);
	}
	
	public static String toStringYYYY_MM_DD(Calendar cal) {
		return toString(cal, YYYY_MM_DD);
	}
	
	/**
	 * 返回 YYYYMMDDHHMMSS 格式字符串
	 * @param date
	 * @return YYYYMMDDHHMMSS 格式字符串
	 */
	public static String toStringYYYYMMDDHHMMSS(Date date) {
//		SimpleDateFormat format = new SimpleDateFormat(YYYYMMDDHHMMSS);
		return toString(date,YYYYMMDDHHMMSS);
	}
	
	/**
	 * 返回 YYYY_MM_DD_HH_MM_SS 格式字符串
	 * @param date
	 * @return YYYY_MM_DD_HH_MM_SS 格式字符串
	 */
	public static String toStringYYYY_MM_DD_HH_MM_SS(Date date) {
		//SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return toString(date,YYYY_MM_DD_HH_MM_SS);
	}
	
	/**
	 * 返回 YYYY_MM_DD_HH_MM 格式字符串
	 * @param date
	 * @return YYYY_MM_DD_HH_MM 格式字符串
	 */
	public static String toStringYYYY_MM_DD_HH_MM(Date date) {
		//SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return toString(date,YYYY_MM_DD_HH_MM);
	}

	/**
	 * 返回 YYYY_MM_DD_HH_MM_SS 格式字符串
	 * @param date
	 * @return YYYY_MM_DD_HH_MM_SS_SSS 格式字符串
	 */
	public static String toStringYYYY_MM_DD_HH_MM_SS_SSS(Date date) {
//		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SSS);
		return toString(date,YYYY_MM_DD_HH_MM_SS_SSS);
	}

	/**
	 * 将字符串 dateStr 按照 格式 formatStr 转换成date
	 * @param formatStr
	 * @param dateStr
	 * @return Date
	 * @throws ParseException
	 */
	public static Date toDate(String dateStr,String formatStr) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.parse(dateStr);
	}
	
	/**
	 * YYYY_MM_DD 格式的字符串转换成date
	 * @param date
	 * @return Date
	 * @throws ParseException
	 */
	public static Date toDateYYYY_MM_DD(String date) throws ParseException {
		return toDate(date,TimeUtil.YYYY_MM_DD);
	}

	/**
	 * 将 date 格式化 YYYY_MM_DD_HH_MM_SS 之后 返回 指定格式的 新 Date
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date toDate(Date date) throws ParseException {
		return toDate(date,TimeUtil.YYYY_MM_DD_HH_MM_SS);
	}
	
	/**
	 * 将 date 格式化之后 返回 指定格式的 新 Date
	 * @param date
	 * @param formatStr
	 * @return
	 * @throws ParseException
	 */
	public static Date toDate(Date date,String formatStr) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		String dateStr = format.format(date);
		return format.parse(dateStr);
	}

	public  static Boolean isTimeOut(Date startDate,Short delayMonths)throws ParseException {
		//计算到期时间
		Date endDate= TimeUtil.getDateAfterMonths(startDate,delayMonths);
		endDate = toDate(endDate, TimeUtil.YYMMDD);
		Date now = toDate(new  Date(), TimeUtil.YYMMDD);
		if(endDate.after(now)){
			return false;
		}else {
			return  true;
		}
	}
	/**
	 * 将 Calendar 格式化之后 返回 指定格式的 Date
	 * @param calendar
	 * @param formatStr
	 * @return
	 * @throws ParseException
	 */
	public static Date toDate(Calendar calendar,String formatStr) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		String dateStr = format.format(calendar.getTime());
		return format.parse(dateStr);
	}
	
	/**
	 * YYYY_MM_DD_HH_MM_SS_SSS 格式的字符串转换成date
	 * @param dateStr
	 * @return Date
	 * @throws ParseException
	 */
	public static Date toDateYYYY_MM_DD_HH_MM_SS_SSS(String dateStr) throws ParseException {
//		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SSS);
		return toDate(dateStr,TimeUtil.YYYY_MM_DD_HH_MM_SS_SSS);
	}

	/**
	 * YYYY_MM_DD_HH_MM_SS 格式的字符串转换成date
	 * @param dateStr
	 * @return Date
	 * @throws ParseException
	 */
	public static Date toDateYYYY_MM_DD_HH_MM_SS(String dateStr) throws ParseException {
//		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return toDate(dateStr,TimeUtil.YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * YYYYMMDDHHMMSS 格式的字符串转换成date
	 * @param dateStr
	 * @return Date
	 * @throws ParseException
	 */
	public static Date toDateYYYYMMDDHHMMSS(String dateStr) throws ParseException {
//		SimpleDateFormat format = new SimpleDateFormat(YYYYMMDDHHMMSS);
		return toDate(dateStr,TimeUtil.YYYYMMDDHHMMSS);
	}



	public static String toTimeYYYY_MM_DD_HH_MM_SS(Timestamp stamp) {
		return toString(stamp, YYYY_MM_DD_HH_MM_SS);
	}

	public static String toTimeYYYY_MM_DD_HH_MM_SS(Date dt) {
		return toString(dt, YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 将dateStr转化成时间，并格式化成YYYY_MM_DD格式
	 * @param dateStr
	 * @return Timestamp
	 */
	public static Timestamp toTimestampYYYY_MM_DD(String dateStr) {
		return toTimestamp(dateStr, YYYY_MM_DD);
	}

	/**
	 * 将dateStr转化成时间，并格式化成pattern格式
	 * @param dateStr
	 * @param formatStr
	 * @return Timestamp
	 */
	public static Timestamp toTimestamp(String dateStr, String formatStr) {
		Calendar cal = toCalendar(dateStr, formatStr);
		Timestamp stamp = new Timestamp(cal.getTimeInMillis());
		return stamp;
	}

	public static Timestamp toTimestamp(Calendar cal) {
		Timestamp stamp = new Timestamp(cal.getTimeInMillis());
		return stamp;
	}

	public static Calendar copy(Calendar cal) {
		Calendar c = Calendar.getInstance();
		c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
				cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
		c.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
		return c;
	}

	/**
	 * 计算c2 - c1 之间间隔的天数
	 * 
	 * @param c1
	 *            起始日期
	 * @param c2
	 *            终止日期
	 * @return
	 */
	public static double daysBetween(Calendar c1, Calendar c2) {
		long start = c1.getTimeInMillis();
		long end = c2.getTimeInMillis();
		return (end - start) / (24 * 60 * 60 * 1000d);
	}
	
	/**
	 * 计算c2 - c1 之间间隔的天数 YYYY_MM_DD 格式
	 * 
	 * @param c1
	 *            起始日期
	 * @param c2
	 *            终止日期
	 * @return
	 */
	public static double daysBetween(Timestamp c1, Timestamp c2) {
		String stratStr = toString(c1, YYYY_MM_DD);
		String endStr = toString(c2, YYYY_MM_DD);
		Timestamp startstamp = toTimestampYYYY_MM_DD(stratStr);
		Timestamp endtamp = toTimestampYYYY_MM_DD(endStr);

		long start = startstamp.getTime();
		long end = endtamp.getTime();
		return (end - start) / (24 * 60 * 60 * 1000d);
	}

	/**
	 * 保留年月日 时分秒和毫秒置0
	 * 慎用，此方法会改变日期值
	 * @param cal
	 */
	public static void retainYYMMDD(Calendar cal) {
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
	}
	
	/**
	 * 将日期时分秒置为0,此方法输出的是传入的日期时间的clone对象，不会修改原值
	 * @param cal
	 * @return
	 */
	public static Calendar copyAs(Calendar cal) {
		Calendar newCal = (Calendar)cal.clone();
		newCal.set(Calendar.HOUR_OF_DAY, 0);
		newCal.set(Calendar.MINUTE, 0);
		newCal.set(Calendar.SECOND, 0);
		newCal.set(Calendar.MILLISECOND, 0);
		return newCal;
	}

	/**
	 * 按给定的格式获取当前时间：字符串
	 * 
	 * @param formatStr
	 * @return
	 */
	public static String getCurrentTimeAsStr(String formatStr) {
		Calendar c = Calendar.getInstance();
		return new SimpleDateFormat(formatStr).format(c.getTimeInMillis());
	}
	
	/**
	 * 获取当日字符串格式时刻，格式：YYYY_MM_DD_HH_MM_SS
	 * @param ultimate true： 返回最后一秒的时间，否则返回当日0点0分0秒
	 * @return
	 */
	public static String getCurrentDayAsStr(boolean ultimate) {
		StringBuffer cal = new StringBuffer(toStringYYYY_MM_DD(Calendar.getInstance()));
		if(ultimate) {
			return cal.append(" 23:59:59").toString();
		}
		return cal.append(" 00:00:00").toString();
	}

	/**
	 * 获取指定日期偏移delayMonths后的日期
	 * 
	 * @param startDate
	 *            -开始日期
	 * @param delayMonths
	 *            -延迟的月数
	 * @return Date -转换后的日期
	 */
	public static Date getDateAfterMonths(Date startDate, int delayMonths) {
		if (null == startDate) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.add(Calendar.MONTH, delayMonths);
		return c.getTime();
	}

	/**
	 * 获取指定日期偏移delayDays后的日期
	 * 
	 * @param startDate
	 *            -开始日期
	 * @param delayDays
	 *            -延迟的天数
	 * @return Date -转换后的日期
	 */
	public static Date getDateAfterDays(Date startDate, int delayDays) {
		if (null == startDate) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.add(Calendar.DAY_OF_MONTH, delayDays);
		return c.getTime();
	}
	
	/**
	 * 获取指定日期偏移delayDays后的日期
	 * 
	 * @param startDate
	 *            -开始日期
	 * @param second
	 *            -延迟的天数
	 * @return Date -转换后的日期
	 */
	public static Date getDateAfterSecond(Date startDate, int second) {
		if (null == startDate) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.add(Calendar.SECOND, second);
		return c.getTime();
	}

	/**
	 * 一个月最大day
	 * 
	 * @param time
	 *            时间
	 * @return obj[0]=maxMonth; obj[1]=time
	 */
	public static Object[] getMaxMonth(String time) {
		Object[] obj = new Object[2];
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar a = Calendar.getInstance();
		a.setTime(date);
		a.set(Calendar.DATE, 1); // 把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
		int maxMonth = a.get(Calendar.DATE);
		a.roll(Calendar.DATE, 1);
		time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getTime());
		obj[0] = maxMonth;
		obj[1] = time;
		return obj;
	}

	/**
	 * 处理时间的加减运算 60*60 为一个小时 60*60*24 为一天
	 * 
	 * @param startTime
	 * @param endTime
	 * @param type
	 *            0为加 1为减
	 * @return Date long
	 */
	public static long getDateAdd(String startTime, String endTime, int type) {
		long time = 0L;
		try {
			Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
			Date addLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
			switch (type) {
			case 0:
				time = (date.getTime() / 1000) + (addLong.getTime() / 1000);
				break;
			case 1:
				time = (date.getTime() / 1000) - (addLong.getTime() / 1000);
				break;
			default:
				time = (date.getTime() / 1000) + (addLong.getTime() / 1000);
				break;
			}
			date.setTime(time * 1000);
			time = date.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 时间差
	 * 
	 * @param current_time
	 *            当前时间
	 * @param compare_time
	 *            比较时间
	 * @return long
	 */
	public static long getDateCompare(String current_time, String compare_time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long time = 0;
		try {
			Date c_tiem = sf.parse(current_time);
			Date com_time = sf.parse(compare_time);
			long l = c_tiem.getTime() - com_time.getTime() > 0 ? c_tiem.getTime() - com_time.getTime()
					: com_time.getTime() - c_tiem.getTime();
			time = l / 1000; // 算出超时秒数
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 获取日期方法
	 * 
	 * @param type
	 *            0:yyyy-MM-dd HH:mm:ss 1:yyyyMMddHHmmss 2:yyyyMMdd
	 * @param formatStr
	 *            日期格式
	 * @return String
	 */
	public static String getDate(int type, String formatStr) {
		Date date = new Date();
		SimpleDateFormat sdf = null;
		if (null != formatStr) {
			sdf = new SimpleDateFormat(formatStr);
		} else if (type == 0) {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else if (type == 1) {
			sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		} else if (type == 2) {
			sdf = new SimpleDateFormat("yyyyMMdd");
		}
		String str = sdf.format(date);
		return str;
	}
	
	/**
	 * 获取给定日期的前一天最有后一秒的时间（如：给定时间是2017-11-02 10:00:00,返回值为：2017-11-01 23:59:59）
	 * @param cal
	 * @return
	 */
	public static Calendar getDayBeforeLastMinutes(Calendar cal) {
		Calendar c2 = (Calendar)cal.clone();
		c2.add(Calendar.DATE, -1);
		c2.set(Calendar.HOUR_OF_DAY, 23);
		c2.set(Calendar.MINUTE, 59);
		c2.set(Calendar.SECOND, 59);
		return c2;
	}
	
	/**
     * 将时间由秒转换成指定格式，如(long转：yyyy-MM-dd HH:mm:ss)
     * @param second
     * @param format
     * @return
     * @throws Exception
     */
    public static Date transferLongToDate(Long second, String format)
    {
        if(second==null||second==0){
            return null;
        }
        Date da = null;
        try {
            da = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("1970-01-01 08:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date date = new Date(da.getTime() + second * 1000L);
        return date;
    }

	/**
	 * 计算时间差
     * 发布时间显示规则：
     * 当时10分钟内发布的显示：刚刚
     * 当日10分钟后发布的只显示  时：分
     * 超过当日发布的显示：昨天  时：分
     * 超过昨天发布的显示：前天  时;分
     * 超过今年发布的显示：年-月-日  时：分
	 * end大于begin
	 */
	public static String getTimeDifference(Date begin,Date end) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(begin);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        String dateDesc = startCalendar.get(Calendar.HOUR_OF_DAY)+":"+startCalendar.get(Calendar.MINUTE);
        try {
            int startYear = startCalendar.get(Calendar.YEAR);
            int startMonth = startCalendar.get(Calendar.MONTH)+1;
            int startDate = startCalendar.get(Calendar.DATE);
            int startHour = startCalendar.get(Calendar.HOUR);
            int startMinute = startCalendar.get(Calendar.MINUTE);

            int endYear = endCalendar.get(Calendar.YEAR);
            int endMonth = endCalendar.get(Calendar.MONTH)+1;
            int endDate = endCalendar.get(Calendar.DATE);
            int endHour = endCalendar.get(Calendar.HOUR);
            int endMinute = endCalendar.get(Calendar.MINUTE);

            int month = (startCalendar.get(Calendar.MONTH)+1);
            int date = startCalendar.get(Calendar.DATE);
            int hour = startCalendar.get(Calendar.HOUR_OF_DAY);
            int minute = startCalendar.get(Calendar.MINUTE);
            String month_date = (month<10 ? ("0"+month):month) +"-"+ (date<10 ? ("0"+date):date);
            String hour_minute = (hour<10 ? ("0"+hour):hour) + ":" + (minute<10 ? ("0"+minute):minute);
            String month_date_hour_minute = month_date+" "+hour_minute;
            String year_month_date_hour_minute = startCalendar.get(Calendar.YEAR)+"-"+month_date_hour_minute;
            //同年同月,并且需要保证endTime>startTime
            if(endYear==startYear) {
                if(endMonth==startMonth) {
                    //年-月-日 时相等,10分钟之内显示刚刚
                    if ((endDate == startDate) && (startHour==endHour) && (endMinute-startMinute) < 10) {
                        dateDesc = "刚刚";
                    }else if((endDate == startDate)) {
                        dateDesc = hour_minute;
                    }else if ((endDate - startDate) == 1) {
                        dateDesc = "昨天" +" "+ hour_minute;
                    } else if ((endDate - startDate) == 2) {
                        dateDesc = "前天" +" "+ hour_minute;
                    } else {
                        dateDesc = month_date_hour_minute;
                    }
                }else if(endMonth>startMonth){
                    dateDesc = month_date_hour_minute;
                }
            }else if(endYear>startYear){
                dateDesc = year_month_date_hour_minute;
            }
		}catch (Exception e){
            logger.error("getTimeDifference计算出时间差异常:",e);
        }
        return dateDesc;
	}

	/**
	 * a、本年度时光轴：显示格式月/日，周。示例：6/18  周三
	 * b、其他年份时光轴：显示格式年/月/日，周。示例：2019/6/18  周三
	 * @param dateStr
	 * @return
	 */
	public static String formatDate(String dateStr){
		String []arr = {"周日","周一","周二","周三","周四","周五","周六"};
		Calendar calendar = Calendar.getInstance();
		int currentYear = calendar.get(Calendar.YEAR);
        String dateDesc = "";
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            calendar.setTime(date);
            String month_date_week = (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DATE) + " " + arr[calendar.get(Calendar.DAY_OF_WEEK) - 1];
            String year_month_date_week = calendar.get(Calendar.YEAR) + "/" + month_date_week;

			if(currentYear==calendar.get(Calendar.YEAR)){
				dateDesc = month_date_week;
			}else{
				dateDesc = year_month_date_week;
			}
		}catch (Exception e){
            logger.error("formatDate格式化时间异常:",e);
		}
		return dateDesc;
	}

	public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(beginTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		return date.after(begin) && date.before(end);
	}

    /**
     *  根据年份和月份计算出当月的非周末日
     * @param year
     * @param month
     * @return
     */
    public static List<Date> getDates(int year, int month){
        List<Date> dates = new ArrayList<Date>();

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH,  month - 1);
        cal.set(Calendar.DATE, 1);


        while(cal.get(Calendar.YEAR) == year &&
                cal.get(Calendar.MONTH) < month){
            int day = cal.get(Calendar.DAY_OF_WEEK);

            if(!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)){
                dates.add((Date)cal.getTime().clone());
            }
            cal.add(Calendar.DATE, 1);
        }
        return dates;

    }

    /**
     *  根据年份和月份计算出当月的周末日
     * @param year
     * @param month
     * @return
     */
    public static List<Date> getWeekendDates(int year, int month){
        List<Date> dates = new ArrayList<Date>();

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH,  month - 1);
        cal.set(Calendar.DATE, 1);


        while(cal.get(Calendar.YEAR) == year &&
                cal.get(Calendar.MONTH) < month){
            int day = cal.get(Calendar.DAY_OF_WEEK);

            if(day == Calendar.SUNDAY || day == Calendar.SATURDAY){
                dates.add((Date)cal.getTime().clone());
            }
            cal.add(Calendar.DATE, 1);
        }
        return dates;

    }

	/**
	 *  获取当天指定时间的日期
	 * @param hour
	 * @param minute
	 * @return
	 */
    public static Date getAssignDate(int hour,int minute){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.SECOND, 00);
		return cal.getTime();
	}

	/**
	 *  获取当天时间戳
	 * @return
	 */
	public static Long getCurrentBeginDate() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);//控制时
		cal.set(Calendar.MINUTE, 0);//控制分
		cal.set(Calendar.SECOND, 1);//控制秒
		return cal.getTimeInMillis();
	}

	public static void main(String[] args)
	{
		Date endDate = new Date();
		Date beginDate = TimeUtil.getDateAfterMonths(endDate,-2);
		String strBeginDate = TimeUtil.toStringYYYY_MM_DD(beginDate);
		String strEndDate = TimeUtil.toStringYYYY_MM_DD(endDate);
		System.out.println(strBeginDate);
		System.out.println(strEndDate);
	}


	public static int differentDaysByMillisecond(Date date1,Date date2)
	{
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
		return days;
	}

	public static String compareTime(String comparePeriod) {
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
		String date = sdf.format(new Date());
		try{
			Date now = df.parse(df.format(new Date()));
			Date stat = df.parse(comparePeriod);
			Calendar nowTime = Calendar.getInstance();
			nowTime.setTime(now);
			Calendar statTime = Calendar.getInstance();
			statTime.setTime(stat);
			if (nowTime.before(statTime)){
				//取昨天的数据
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());//把当前时间赋给日历
				calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
				Date dBefore = calendar.getTime();   //得到前一天的时间
				date = sdf.format(dBefore);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}

	public static String dateToWeek(String datetime) {

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance(); // 获得一个日历
		Date datet = null;
		try {
			datet = f.parse(datetime);
			cal.setTime(datet);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
		if (w < 0) {
			w = 0;
		}
		return weekDays[w];
	}

	/**
	 * 当前日期往前推6天
	 */
	public static Date forward6Days(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)-6);
		Date date = calendar.getTime();
		try{
			date = toDate(date,YYYY_MM_DD);
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}

    public static List<String> get7DaysDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        List<String> dates = new LinkedList<>();
        for(int i=1;i<=7;i++){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)-(7-i));
            dates.add(simpleDateFormat.format(calendar.getTime()));
        }
        return dates;
    }

	/**
	 * 获取两个时间间隔分钟
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int getDateTimeBetweenMin(Date startTime, Date endTime){
		long from = startTime.getTime();
		long to = endTime.getTime();
		int minutes = (int) ((to - from)/(1000 * 60));
		return minutes;
	}

	/**
	 * 获取两个时间间隔秒钟
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int getDateTimeBetweenSecond(Date startTime, Date endTime){
		long from = startTime.getTime();
		long to = endTime.getTime();
		int minutes = (int) ((to - from)/(1000));
		return minutes;
	}
}
